﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.WinForms;
using System.Xml;

namespace SMS.Console.Controls
{
    public partial class WebSiteConfigPage : ConfigPage
    {
        public WebSiteConfigPage()
        {
            InitializeComponent();
        }

        public XmlDocument ServiceDocument { get; set; }
        public XmlDocument ConsoleDocument { get; set; }

        private void txt_ServiceUrl_Enter(object sender, EventArgs e)
        {
            txt_ServiceUrl.Text = txt_ServiceUrl.Tag as string;
            txt_ServiceUrl.ForeColor = Color.FromKnownColor(KnownColor.WindowText);
        }

        private void txt_ServiceUrl_Leave(object sender, EventArgs e)
        {
            txt_ServiceUrl.Tag = txt_ServiceUrl.Text;
            if (txt_ServiceUrl.Text == string.Empty)
            {
                txt_ServiceUrl.Text = "请输入短信服务地址";
                txt_ServiceUrl.ForeColor = Color.FromKnownColor(KnownColor.GrayText);
            }
        }

        private void rb_Auto_CheckedChanged(object sender, EventArgs e)
        {
            txt_ClientUrl.Visible = rb_Custom.Checked;
        }

        private void rb_Custom_CheckedChanged(object sender, EventArgs e)
        {
            txt_ClientUrl.Visible = rb_Custom.Checked;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            XmlNode serviceUrlNode = this.ServiceDocument.SelectSingleNode("configuration/system.serviceModel/services/service/host/baseAddresses/add");
            if (serviceUrlNode != null)
            {
                XmlAttribute urlAttr = serviceUrlNode.Attributes["baseAddress"];
                if (urlAttr != null)
                {
                    string url = urlAttr.Value;
                    if (!string.IsNullOrEmpty(url))
                    {
                        txt_ServiceUrl.Text = url;
                        txt_ServiceUrl.Tag = url;
                        txt_ServiceUrl.ForeColor = Color.FromKnownColor(KnownColor.WindowText);
                    }
                }
            }
            XmlNode clientUrlNode = this.ConsoleDocument.SelectSingleNode("configuration/system.serviceModel/client/endpoint");
            if (clientUrlNode != null)
            {
                XmlAttribute urlAttr = clientUrlNode.Attributes["address"];
                if (urlAttr != null)
                {
                    string url = urlAttr.Value;
                    if (string.IsNullOrEmpty(url) || url == txt_ServiceUrl.Text)
                    {
                        rb_Auto.Checked = true;
                        rb_Custom.Checked = false;
                    }
                    else
                    {
                        rb_Auto.Checked = false;
                        rb_Custom.Checked = true;
                        txt_ClientUrl.Text = url;
                    }
                }
            }
        }

        public override bool CanNavigate(SMS.WinForms.WizardFormNavigationEventArgs e)
        {
            if (e.NavigationType == SMS.WinForms.WizardFormNavigationTypes.Next)
            {
                try
                {
                    XmlNode serviceUrlNode = this.ServiceDocument.SelectSingleNode("configuration/system.serviceModel/services/service/host/baseAddresses/add");
                    if (serviceUrlNode != null)
                    {
                        serviceUrlNode.Attributes["baseAddress"].Value = txt_ServiceUrl.Text;
                    }
                    XmlNode clientUrlNode = this.ConsoleDocument.SelectSingleNode("configuration/system.serviceModel/client/endpoint");
                    if (clientUrlNode != null)
                    {
                        var clientUrl = txt_ClientUrl.Text;
                        if (rb_Auto.Checked) clientUrl = txt_ServiceUrl.Text;
                        clientUrlNode.Attributes["address"].Value = clientUrl;
                    }
                }
                catch (Exception ex)
                {
                    ErrorBox.Show(ex);
                    return false;
                }
            }
            return true;
        }
    }
}
